<?php
/*****************
*@供应商管理中心
*@date 14/3/17
******************/
class SupplierAction extends BaseAction {

/*****************
*@Index 用户管理
******************/
	public function index(){
		$mer_user = M($this->dao_supplier.'.mer_user');
		$status = array();
		$status[0] = '禁用';
		$status[1]= '启用';
		$count = $mer_user->count();
		$Page = new Page( $count,$this->listrows);
		$res = $mer_user->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('totalCount',$count);	
		$this->assign('status',$status);
		$this->assign('list',$res);
		$this->display();
	}

/********************
*@Commodity 商品管理
********************/
	public function commodity(){
		$product = M($this->dao_supplier.'.mer_product');
		$deal_status = array();
		$deal_status[0] = '未审核';
		$deal_status[1] = '审核通过';
		$deal_status[-1] = '审核不通过';
		$status = array();
		$status[0] = '已删除';
		$status[1] = '正常';
		$count = $product->count();
		$Page = new Page( $count,$this->listrows);
		$res = $product->limit($Page->firstRow.','.$Page->listRows)->order('id DESC')->select();
		//echo $product->getLastSql();
		$this->assign('totalCount',$count);	
		$this->assign('deal_status',$deal_status);	
		$this->assign('status',$status);	
		$this->assign('list',$res);
		$this->display();
	}

/**********************
*@Release 协助发布
***********************/
	public function release(){
		$pack = M($this->dao_supplier.'.mer_pack_product');
		$status[0] = '未处理';
		$status[1] = '已处理';
		$res = $pack->select();
		$this->assign('status',$status);
		$this->assign('list',$res);
		$this->display();
	}
	
	public function release_status(){
		$pack = M($this->dao_supplier.'.mer_pack_product');
		$id = $_GET['id'];
		if(empty($id)){
			return false;
		}
		$where  = array();
		$where['id'] = $id;
		$res = $pack->where($where)->select();
		$this->assign('list',$res[0]);
		$this->display();
	}
	
	public function release_upd(){
		$pack = M($this->dao_supplier.'.mer_pack_product');
		$id = $_POST['id'];
		$status = $_POST['status'];
		$data = array();
		$where = array();
		if(empty($id)){
			return false;
		}
		$data['deal_status'] = $status;
		$where['id'] = $id;
		$row = $pack->where($where)->save($data);
		if($row>0){
			$this->_ajm(200,'修改成功');
		}else{
			$this->_ajm(300,'修改失败',1);	
		}
	}

/***********************
*@Quotation 商家报价
***********************/
	public function quotation(){
	
		$quote = M($this->dao_supplier.'.mer_product_quote');
		$key = $_REQUEST['keywords'];
		$stype = $_REQUEST['sfield'];
		if(!empty($key) && $stype!='all'){
			$where[$stype] = array('like',"%{$key}%");
		}
		$count = $quote->count();
		$Page = new Page( $count,$this->listrows);
		$order=$this->orderField.' '.$this->orderDirection;
		//$res = $quote->limit($Page->firstRow.','.$Page->listRows)->select();
		$res = $quote->where( $where )->order ( $order )->limit($Page->firstRow.','.$Page->listRows)->
		field('q.addtime,q.id,q.order_id,q.productid,q.mer_productid,q.price,q.nums,q.mer_userid,o.price as oprice,q.`status`,o.product_name,r.sn')->
		join('as q left join order_goods as o on q.productid = o.product_id and q.order_id =o.order_id left join `order` as r on q.order_id = r.id')
		->select();
		$this->assign( $_REQUEST );
		$this->assign('wherefields',$stype);
		$this->assign('totalCount',$count);	
		$this->assign('list',$res);
		$this->display();
	}
	
	public function add(){
	
		$id = $_GET['id'];
		$uid = $_GET['uid'];
		$where = array();
		$where['id'] = $id;
		$where['mer_userid'] = $uid;
		$list = array();
		$list['mer_productid'] = $id;
		$list['mer_userid'] = $uid;
		$list2  =array();
		$product = M($this->dao_supplier.'.mer_product');
		$mer = M($this->dao_supplier.'.mer_product_connection');
		$res = $product->where($where)->select();
		$data = $mer->where($list)->find();
		$list2['p.id'] = $data['productid'];
		$list2['s.mer_userid'] = $data['mer_userid'];
		$title = $res[0]['title'];
		$id = $res[0]['id'];
		$mer_userid= $res[0]['mer_userid'];
		//$data = $this->product_gl($title); 
		$data = $this->select($list2);
		$this->assign('title',$title);
		$this->assign('id',$id);
		$this->assign('mer_userid',$mer_userid);
		$this->assign('list',$data);
		$this->display();
	}
	
	public function select($where = array()){
		
		$mer = M($this->dao_supplier.'.mer_product_connection');
		$mer_product = $this->dao_supplier.'.mer_product';
		$erp = $this->dao_erp.'.product';
		$res = $mer->field('p.name')->join("as s inner join {$erp} as p on s.productid = p.id")->limit( 1 )->where( $where )->select();
		if(!empty( $res )){
				return $res;
		}else{
				return false;
		}
	}
	
	public function product_gl($title){
		if(empty($title)){
				return false;
		}
		$where['name'] = array('like',"%{$title}%");
		$product = 	M('product');
		$res = $product->field('id,name')->where($where)->select();
		if($res){
			return $res;
		}else{
			return false;
		}
	}
	
	
	public function insert(){
	
		//$ids = $_POST['ids'];
		$id = intval($_POST['id']);
		$userid = intval($_POST['mer_userid']);
		$ids = intval($_POST['keywords_id']);
		$connection = M($this->dao_supplier.'.mer_product_connection');
		if(empty($ids) || $ids <= 0){
			$this->_ajm(300,'请选择关联的产品',1);
			return false;
		}
		$where = array();
		$data = array();
		$data['productid'] = $ids;
		$data['mer_productid'] = $id;
		$data['mer_userid'] = $userid;
		$data['addtime'] = time();
		$data['status'] = 1;
		$num = 1;
		$where['productid'] = $ids;
		$where['mer_productid'] = $id;
		$list = array();
		$list['mer_productid'] = $id;
		$data2 = array();
		$data2['productid'] = $ids;
		$res = $connection->where($where)->select();
		if($res == null){
			$row = $connection->add($data);
				if($row>0){
					$this->update($id,$num);
					$this->_ajm(200,'产品关联成功！');
				}else{
					$connection->where($list)->data($data2)->save();
					$this->_ajm(200,'修改成功！',1);
				}
		}
		
	}
	
	
	public function update($id,$status){
		if(empty($id) && !is_numeric($id)){
				return false;
		}
		if(empty($status) && !is_numeric($status)){
				return false;	
		}	
		$data = array();
		$where = array();
		$where['id'] = $id;
		$product = M($this->dao_supplier.'.mer_product');
		$data['deal_status'] = $status;
		$row = $product->where($where)->save($data);
		if($row>0){
			return true;
		}else{
			return false;
		}
	}
	
	
	public function user_add(){
		
		$product = M($this->dao_supplier.'.mer_user');
		$data = $_POST['base'];
		if(!empty($data)){
			$data['password'] = md5($data['password']);
			$data['supplier_phone']=trim($data['supplier_phone']);
			if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/",$data['supplier_phone'])){   
	             $row = $product->add($data);
				if($row>0){
					$this->_ajm(200,'添加成功');
				}
			}else{   
	         	$this->_ajm(300,'请输入正确的手机号',1);
				return false;
			} 
			unset($data);  
		}	
		$this->display();	
	}
	
	
	public function product_add(){
		$product = M($this->dao_supplier.'.mer_product');
		$data = $_POST['base'];
		$id = $_GET['id'];
		if(!empty($data)){
			if(! is_numeric($data['price']) && !empty($data['price'])){
				$this->_ajm(300,'价格必须是整数',1);
				return false;
			}
		$row = $product->add($data);
			if($row>0){
				$this->_ajm(200,'添加成功');
			}else{
				$this->_ajm(300,'添加失败',1);	   
			}
		}
		$this->assign('id',$id);
		$this->display();
	}
	
	
	public function product_order(){
		$product = M($this->dao_supplier.'.mer_product_connection');
		$res = $product->join('as m inner join order_goods as g on m.productid = g.product_id inner join `order` as o on g.order_id = o.id')->select();
		$this->assign('list',$res);
		$this->display();
	}
	
	public function sel_user(){
		$id = $_GET['id'];
		if(empty($id)){
			return false;
		}
		$where['mer_userid'] = $id;
		$mer_user = M($this->dao_supplier.'.mer_user');
		$status = array();
		$status[0] = '禁用';
		$status[1]= '启用';
		$count = $mer_user->count();
		$Page = new Page( $count,$this->listrows);
		$res = $mer_user->where($where)->find();
		$this->assign('totalCount',$count);	
		$this->assign('status',$status);
		$this->assign('list',$res);
		$this->display();
	}
	

}
?>